#!/bin/sh

########################################################################
# This test checks if kedr_save_stack_trace() allows to obtain at least
# one reliable stack frame. Actually, it just allows to see if the 
# underlying mechanism crashes or not. 'addr_foo' parameter and the like 
# are currently not used.
# 
# Usage: 
#   sh test.sh
########################################################################

########################################################################
# A function to check prerequisites: whether the necessary files exist,
# etc.
########################################################################
checkPrereqs()
{
    if test ! -f "${TEST_MODULE}"; then
        printf "Stack trace module is missing: ${TEST_MODULE}\n"
        exit 1
    fi
}

########################################################################
# Cleanup function
########################################################################
cleanupAll()
{
    @RMMOD@ "${TEST_MODULE_NAME}" || true
}

########################################################################
# doTest() - preform the actual testing
########################################################################
doTest()
{
    @INSMOD@ "${TEST_MODULE}"
    if test $? -ne 0; then
        printf "Failed to load ${TEST_MODULE}\n"
        cleanupAll
        exit 1
    fi
    
    OUT_PARAM_FILE="/sys/module/${TEST_MODULE_NAME}/parameters/stack_frames"
    if test ! -e "${OUT_PARAM_FILE}"; then
        printf "Parameter file does not exist: ${OUT_PARAM_FILE}\n"
        cleanupAll
        exit 1
    fi

    # Save and analyze the result.
    stack_frames=$(cat "${OUT_PARAM_FILE}")

    cleanupAll
    
    if test ${stack_frames} -eq 0; then
        printf "Unspecified error: no stack frames have been obtained."
        exit 1
    fi

#    if test ${stack_frames} -eq 1; then
#        printf "Only one stack frame has been obtained. "
#        printf "save_stack_trace() failed to provide reliable stack trace.\n"
#        exit 1
#    fi
# Even if the system dies not support obtaining reliable stack traces, 
# kedr_save_stack_trace() must provide at least one reliable frame. 
# This is enough in many cases.

    printf "Number of reliable stack frames obtained: ${stack_frames}\n"
}

########################################################################
# main
########################################################################
if test $# -ne 0; then
    printf "Usage: sh test.sh\n"
    exit 1
fi

TEST_MODULE_NAME=@KMODULE_NAME@
TEST_MODULE=${TEST_MODULE_NAME}.ko

checkPrereqs
doTest

# test passed
exit 0
